package io;

import utils.SaveLoad;
import generator.return_SC_randomsequences;

import java.io.File;
import java.util.*;

public class FastaReadWrite {

	private static final String fastaFile = "sequences.fa";
	private static final int LINELEN = 80;
	private static 		int numSeq = 0;

	public static void main(String args[])
	{
		write(return_SC_randomsequences.returnSCSequences(10, 100));
		String [] contents = read();
		for (String c : contents)
			System.out.println(c);
	}
	
	/**
	 * Reads and returns scSequences from a file
	 */
	public static String [] read()
	{
		String readContents = SaveLoad.loadFile(fastaFile);
		String [] lineSeparatedContent = readContents.split("\n");
		
		Vector<String> vectorizedContent = new Vector<String>();
		String currContent = "";
		for (String line : lineSeparatedContent)
		{
			if (line.contains(">"))
			{
				if (!currContent.isEmpty()) 
					vectorizedContent.add(currContent);
				currContent = "";
			} // start new sequence
			else
			{
				currContent += line;
			} //append onto existing sequence
		}
		
		// string [] is universal format so change to universal
		String [] returner = new String[vectorizedContent.size()];
		for (int idx = 0; idx < vectorizedContent.size(); idx++)
			returner[idx] = vectorizedContent.get(idx);
		return returner;
	}
	
	
	public static void read(Vector<String> sequence, Vector<Integer> idx, Vector<String> motifString, 
			Vector<Integer> motifLen, Vector<Integer> duplicates)
	{
		String readContents = SaveLoad.loadFile(fastaFile);
		String [] lineSeparatedContent = readContents.split("\n");
		
		String currContent = "";
		for (String line : lineSeparatedContent)
		{
			if (line.contains(">"))
			{
				System.out.println("" + line);
				if (!currContent.isEmpty()) 
					sequence.add(currContent);
				currContent = "";
				String [] currLine = line.split("\\|");
				idx.add(Integer.parseInt(currLine[1])); // idx
				motifString.add(currLine[2]);
				motifLen.add(Integer.parseInt(currLine[3]));
				duplicates.add(Integer.parseInt(currLine[6]));
			} // start new sequence
			else
			{
				currContent += line;
			} //append onto existing sequence
		}
		if (!currContent.isEmpty()) 
			sequence.add(currContent);
	}
	
	
	/**
	 * Writes an sc sequence into sequences.fa file
	 * @param scSequences
	 */
	public static void write(String [] scSequences)
	{
		File f = new File(fastaFile);
		if (f.exists())
		{
			f.delete();
		}
		int numSeq = 0;
		String seqHead = "Sequence_";
		for (String seq : scSequences)
		{
			SaveLoad.saveFile(fastaFile, ">" + seqHead + numSeq + "\n");
			for (int i = 0; i < seq.length() / LINELEN + 1; i++)
			{
				String cum = seq.substring(i * LINELEN, 
						((i+1) * LINELEN < seq.length()) ? 
								(i+1) * LINELEN : 
								seq.length());
				SaveLoad.saveFile(fastaFile, cum + "\n");
			}
			numSeq++;
		}
	}
	
	public static void write(String [] scSequences, int [] idx, String [] motifString, double icpc, int ml, int sc)
	{
		
		File f = new File(fastaFile);
		/*
		if (f.exists())
		{
			f.delete();
		}
		*/
		String seqHead = "Sequence_";
		for (int s = 0; s < scSequences.length; s++)
		{
			String seq = scSequences[s];
			// seqNum, beginBindIdx, motifString, motifStringLen, icpc, ml, sc
			SaveLoad.saveFile(fastaFile, ">" + seqHead + numSeq + "|" + idx[s] 
			                                 + "|" + motifString[s] + "|" + motifString[s].length() 
			                                 + "|" + icpc + "|" + ml + "|" + sc +"\n");
			for (int i = 0; i < seq.length() / LINELEN + 1; i++)
			{
				String cum = seq.substring(i * LINELEN, 
						((i+1) * LINELEN < seq.length()) ? 
								(i+1) * LINELEN : 
								seq.length());
				SaveLoad.saveFile(fastaFile, cum + "\n");
			}
			numSeq++;
		}
	}
}
